<?php
/*
	[BuyPg] (C) 2010-2011 Buypg
	$Id: ShoppingNewsAction.class.php 2012-03-29 23:34:12 wufujun
*/

if (!defined('THINK_PATH')){
	exit('Access Denied');
}

class ShoppingNewsAction extends BpAction{
	function _filter(&$map){
		$map['title'] = array('like',"%".$_POST['title']."%");
	}
	
        //赋值可用分类
        public function _before_add(){
		$YouhuiClassModel = D('YouhuiClass');
		$YouhuiClass = $YouhuiClassModel->where('status = 1')->select();
		$this->assign('YouhuiClass',$YouhuiClass);
	}
        
        //赋值可用分类
	public function _before_edit(){
		$YouhuiClassModel = D('YouhuiClass');
		$YouhuiClass = $YouhuiClassModel->where('status = 1')->select();
		$this->assign('YouhuiClass',$YouhuiClass);
	}
        
	public function insert(){
		$model = D ('ShoppingNews');
		//处理更新日期
		$_POST['dateline'] = empty($_POST['dateline']) ? time() : strtotime($_POST['dateline']);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$list=$model->add ();
		if ($list!==false) {
			$this->success('新增成功!');
		} else {
			//失败提示
			$this->error ('新增失败!');
		}
	}
	
	public function update(){
		$model = D('ShoppingNews');
		//处理更新日期
		$_POST['dateline'] = empty($_POST['dateline']) ? time() : strtotime($_POST['dateline']);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$list=$model->save();
		if ($list!==false) {
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}
	
	//编辑器上传功能
	public function editorupload(){
		$model = 'shoppingnews';
		$php_path = APP_USER . '/attachment/';
		$php_url = APP_URL . '/attachment/';

		//文件保存目录路径
		$save_path = $php_path.$model.'/';
		//创建模块附件文件夹
		if(!file_exists($save_path)){
			mkdir($save_path);
		}
		//文件保存目录URL
		$save_url = $php_url.$model.'/';
		//定义允许上传的文件扩展名
		$ext_arr = array(
			'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
			'flash' => array('swf', 'flv'),
			'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
			'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
		);
		//最大文件大小
		$max_size = 2000000;

		$save_path = realpath($save_path) . '/';

		//有上传文件时
		if (empty($_FILES) === false) {
			//原文件名
			$file_name = $_FILES['imgFile']['name'];
			//服务器上临时文件名
			$tmp_name = $_FILES['imgFile']['tmp_name'];
			//文件大小
			$file_size = $_FILES['imgFile']['size'];
			//检查文件名
			if (!$file_name) {
				alert("请选择文件。");
			}
			//检查目录
			if (@is_dir($save_path) === false) {
				alert("上传目录不存在。");
			}
			//检查目录写权限
			if (@is_writable($save_path) === false) {
				alert("上传目录没有写权限。");
			}
			//检查是否已上传
			if (@is_uploaded_file($tmp_name) === false) {
				alert("临时文件可能不是上传文件。");
			}
			//检查文件大小
			if ($file_size > $max_size) {
				alert("上传文件大小超过限制。");
			}
			//检查目录名
			$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
			if (empty($ext_arr[$dir_name])) {
				alert("目录名不正确。");
			}
			//获得文件扩展名
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			//检查扩展名
			if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
				alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
			}
			//创建文件夹
			if ($dir_name !== '') {
				$save_path .= $dir_name . "/";
				$save_url .= $dir_name . "/";
				if (!file_exists($save_path)) {
					mkdir($save_path);
				}
			}
			//以年/月/日格式创建目录
			$y = date("Y");
			$save_path .= $y . "/";
			$save_url .= $y . "/";
			if(!file_exists($save_path)){
				mkdir($save_path);
			}
			$m = date("m");
			$save_path .= $m . "/";
			$save_url .= $m . "/";
			if(!file_exists($save_path)){
				mkdir($save_path);
			}
			$d = date("d");
			$save_path .= $d . "/";
			$save_url .= $d . "/";
			if (!file_exists($save_path)) {
				mkdir($save_path);
			}
			//新文件名
			$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
			//移动文件
			$file_path = $save_path . $new_file_name;
			if (move_uploaded_file($tmp_name, $file_path) === false) {
				alert("上传文件失败。");
			}
			@chmod($file_path, 0644);
			$file_url = $save_url . $new_file_name;
	
			header('Content-type: text/html; charset=UTF-8');
			echo json_encode(array('error' => 0, 'url' => $file_url));
			exit;
		}
	}
}

?>
